/**
 * 
 */
package com.util;


import java.util.Random;
import android.graphics.Bitmap;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import com.clases.Lugar;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.util.LugarManager.CreateBlurAsync;


/**
 * @author Chris
 * 
 */
public class CoverManager {

    public static boolean isRunning = true;

    private final Lugar[] listaLugares;

    private final Handler mHandler = new Handler();

    private final ImageView portada;

    private String Url = "";

    public CoverManager(Lugar[] listaLugares, ImageView portada) {

        super();
        this.listaLugares = listaLugares;
        this.portada = portada;
    }

    public void startChecking() {

        changeCover();
        //
        new Thread(new Runnable() { // $codepro.audit.disable

            // disallowUnnamedThreadUsage

            @Override
            public void run() {

                // TODO Auto-generated method stub
                while (isRunning) {
                    try {
                        Thread.sleep(5000);
                        mHandler.post(new Runnable() {

                            @Override
                            public void run() {

                                // TODO Auto-generated method stub
                                // Write your code here to update the UI.
                                changeCover();

                            }
                        });
                    }
                    catch (Exception e) {
                        Log.e("startInternetCheck", e.getMessage()
                                    + "");
                    }
                }
            }
        }).start();
    }

    private void changeCover() {

        Random rand = new Random();

        int x = rand.nextInt(listaLugares.length);

        String Urls = listaLugares[x].getPortada();

        if (listaLugares.length > 1) {
            while (Urls.equals(Url)) {
                x = rand.nextInt(listaLugares.length);
                Urls = listaLugares[x].getPortada();
            }

        }
        else
            isRunning = false;

        Url = Urls;

        ImageView cachePortada = new ImageView(portada.getContext());

        descargarImagen(cachePortada, Urls);

    }

    private void descargarImagen(ImageView imagen, String Url) {

        ImageLoader img = ImageLoader.getInstance();

        img.displayImage(Url, imagen, new ImageLoadingListener() {

            @Override
            public void onLoadingCancelled(String imageUri, View view) {

            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

                if (loadedImage != null) {

                    CreateBlurAsync cbs = new CreateBlurAsync(20, loadedImage, portada);
                    cbs.setUserProfile(true);
                    cbs.execute();

                }
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingStarted(String imageUri, View view) {

            }

        });

    }

}
